﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Consultant.Models
{
    public class OperatorRepository
    {
        private ProjectDataContext _datacontext;
        public OperatorRepository(ProjectDataContext datacontext)
        {
            _datacontext = datacontext;
        }
        /// <summary>
        /// Получение ключа по имени пользователя
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public Guid GetUserId(string UserName)
        {
            return _datacontext.aspnet_Users.SingleOrDefault(u => u.UserName == UserName).UserId;
        }

        public void CreateOperator(Operator o)              //переменная бублик или дырка от него же;
        {
            _datacontext.Operators.InsertOnSubmit(o);
            _datacontext.SubmitChanges();
        }

        //Получения статуса оператора по его имени
        public class GetOperators
        {
            public GetOperators(string First_Name, string Last_name, string operator_id)
            {
                FName = First_Name;
                LName = Last_name;
                Id = operator_id;
            }
            public string FName;
            public string LName;
            public string Id;
        }
        public List<GetOperators> GetOnLineOperator()
        {
            IQueryable <Operator> operators = _datacontext.Operators.Where(n => n.OnLine == 1);
            List<GetOperators> res = new List<GetOperators>();
            foreach (Operator op in operators)
            {
                GetOperators r = new GetOperators(op.FirstName, op.LastName, op.UserId.ToString());
                res.Add(r);
            }
            return res;
            //return (int)_datacontext.Operators.SingleOrDefault(e => e.userName == userName).OnLine;
        }
        public bool IsAnybodyHome()
        {
            int? IsOnline = null;
            
            //IsOnline = _datacontext.Operators.SingleOrDefault(o => o.OnLine == 1).OnLine;
            //if (IsOnline == null)
            try { 
                IsOnline = _datacontext.Operators.SingleOrDefault(o => o.OnLine == 1).OnLine;
                return true;
            }
            catch {
                return false;
            }  
        }
        public bool SetOnline(string operator_name)
        {
            _datacontext.Operators.SingleOrDefault(o => o.userName == operator_name).OnLine = 1;
            _datacontext.SubmitChanges();
            return true;
        }
        public bool SetOffline(string operator_name)
        {
            _datacontext.Operators.SingleOrDefault(o => o.userName == operator_name).OnLine = 0;
            return true;
        }
        /// <summary>
        /// Получение оператора по ИД
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Operator GetOperatorById(int id)
        {
            return _datacontext.Operators.SingleOrDefault(e => e.OperatorId == id);
        }

        public int GetIdByUserName(string name)
        {
            return _datacontext.Operators.SingleOrDefault(e => e.userName == name).OperatorId;
        }

        public bool SeeOperatorInSet(string name)
        {
            var t = _datacontext.SetOpAtSites.SingleOrDefault(e=> e.OperId == GetIdByUserName(name));
            if (t == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void SetQueryForSite(SetOpAtSite s)
        {
            _datacontext.SetOpAtSites.InsertOnSubmit(s);
            _datacontext.SubmitChanges();
        }
    }
}